
/** easyui弹框*/
/**
 * 通用弹框
 * id  元素唯一属性
 * options 基础配置, {}
 * buttonsIdx  索引：工具栏按钮配置(快速配置),[0,1,2,3,...]
 * buttonsFunc 事件：工具栏按钮配置(快速配置),[func1,func2,func3,..]
 */
zUtil.dialogInit = function(id,options,buttonsIdx,buttonsFunc,openFlag){
    var bts = [{
        text:'保存',
        id:'z-dialog-save',//0
//        handler:function(){$(id).dialog('close')}
    },{
        text:'关闭',
        id:'z-dialog-close',//1
//        handler:function(){$(id).dialog('close')}
    },{
        text:'同步',
        id:'z-dialog-sync',//2
//        handler:function(){$(id).dialog('close')}
    },{
        text:'保存',
        id:'z-dialog-save2',//3
    },{
        text:'确认',
        id:'z-dialog-save3',//4
    }];
    
    var newbts = [];
    if('undefined' !== $.type(buttonsIdx) && buttonsIdx != null){
        for (var i=0,ln = buttonsIdx.length ;i< ln; i++) {
            var tmp = bts[buttonsIdx[i]];
            if(buttonsFunc && buttonsFunc[i]){
                tmp.handler = buttonsFunc[i];
            }
            newbts.push(tmp);
        }
    }
    //自动创建弹框时时必须传入ID,#xxxx
    if($(id).length==0){
        $('body').append('<div id="'+id.replace('#','')+'" style="display:none;"></div>');
    }
    
    var opts = $.extend(true,{
//         href: '',
        title: ' ',
        cls: 'text-center',
        width: '100%',
        height: '100%',
        maxWidth: '100%',
        maxHeight: '100%',
        closed: openFlag?false:true,
        closable: true,
        cache: false,
        draggable: false,
        modal: true,
        constrain: true,//限制边界
//         toolbar:[{
//             text:'栏目1',
//             handler:function(){}
//         }],
//        buttons: []
    },options);
    
    if('undefined' !== $.type(options.buttons)){
        newbts = $.merge(newbts ,options.buttons);
    }
    if(newbts.length > 0){
        opts.buttons = newbts;
    }
    if(opts.left != null || opts.top != null || opts.right != null || opts.bottom != null){
        return $(id).dialog(opts);
    }
    return $(id).dialog(opts).dialog("center");
};
/**
 * 通用查询弹框
 * id  元素唯一属性
 * options 基础配置, {}
 * buttonsIdx  索引：工具栏按钮配置(快速配置),[0,1,2]
 * buttonsFunc 事件：工具栏按钮配置(快速配置),[func1,func2,func3]
 */
zUtil.queryInit = function(id,options,buttonsIdx,buttonsFunc,openFlag){
    var bts = [{
        text:'查询',
        id:'z-dialog-query',//0
//        handler:function(){$(id).dialog('close')}
    },{
        text:'清空',
        id:'z-dialog-clear',//1
//        handler:function(){$(id).dialog('close')}
    },{
        text:'关闭',
        id:'z-dialog-close',//2
//        handler:function(){$(id).dialog('close')}
    }];
    
    var newbts = [];
    if('undefined' !== $.type(buttonsIdx) && buttonsIdx != null){
        for (var i=0,ln = buttonsIdx.length ;i< ln; i++) {
            var tmp = bts[buttonsIdx[i]];
            if(buttonsFunc && buttonsFunc[i]){
                tmp.handler = buttonsFunc[i];
            }
            newbts.push(tmp);
        }
    }
    //自动创建弹框时时必须传入ID,#xxxx
    if($(id).length==0){
        $('body').append('<div id="'+id.replace('#','')+'" style="display:none;"></div>');
    }
    
    var opts = $.extend(true,{
        title: ' ',
        width: 400,
        height: 'auto',
        maxHeight: '80%',
        minHeight:'120px',
        top : 20,
//        left : 20,
        closed: openFlag?false:true,
        closable: true,
        cache: false,
        draggable: true,
        resizable : false,
        modal: true,
        constrain: true,//限制边界
//        inline:true,
//        buttons: [],
    },options);
    
    if('undefined' !== $.type(options.buttons)){
        newbts = $.merge(newbts ,options.buttons);
    }
    if(newbts.length > 0){
        opts.buttons = newbts;
    }
    if(opts.left != null || opts.top != null || opts.right != null || opts.bottom != null){
        return $(id).dialog(opts);
    }
    return $(id).dialog(opts).dialog("center");
};

zUtil.dialogInit2 = function(id,options,buttonsIdx,buttonsFunc,openFlag){
    var bts = [{
        text:'保存',
        id:'z-dialog-save',//0
//        handler:function(){$(id).dialog('close')}
    },{
        text:'关闭',
        id:'z-dialog-close',//1
//        handler:function(){$(id).dialog('close')}
    },{
        text:'同步',
        id:'z-dialog-sync',//2
//        handler:function(){$(id).dialog('close')}
    }];
    
    var newbts = [];
    if('undefined' !== $.type(buttonsIdx) && buttonsIdx != null){
        for (var i=0,ln = buttonsIdx.length ;i< ln; i++) {
            var tmp = bts[buttonsIdx[i]];
            if(buttonsFunc && buttonsFunc[i]){
                tmp.handler = buttonsFunc[i];
            }
            newbts.push(tmp);
        }
    }
    //自动创建弹框时时必须传入ID,#xxxx
    if($(id).length==0){
        $('body').append('<div id="'+id.replace('#','')+'" style="display:none;"></div>');
    }
    
    var opts = $.extend(true,{
        title: ' ',
        width: 400,
        height: 'auto',
        maxHeight: '80%',
        top : 100,
        closed: openFlag?false:true,
        closable: true,
        cache: false,
        draggable: true,
        resizable : false,
        modal: true,
        constrain: true,//限制边界
//        buttons: [],
    },options);
    
    if('undefined' !== $.type(options.buttons)){
        newbts = $.merge(newbts ,options.buttons);
    }
    if(newbts.length > 0){
        opts.buttons = newbts;
    }
    if(opts.left != null || opts.top != null || opts.right != null || opts.bottom != null){
        return $(id).dialog(opts);
    }
    return $(id).dialog(opts).dialog("center");
};

zUtil.dialogOpen = function(id){
    try{
        $(id).dialog('open');
    }catch(e){
    }
};
zUtil.dialogClose = function(id){
    try{
        $(id).dialog('close');
    }catch(e){
    }
};
zUtil.dialogMove = function(left,top){
    var winObj = $(this).dialog("window");
    var w1 = winObj.outerWidth();
    var h1 = winObj.outerHeight();
    var w2 = winObj.parent().innerWidth();
    var h2 = winObj.parent().innerHeight();
    var flag =false;
    if(top <= 0){
        top = 10;
        flag = true;
    }
    if(left <= 0){
        left = 10;
        flag = true;
    }
    if(top + h1 >= h2){
        top = h2-h1-10;
        flag = true;
    }
    if(left + w1 >= w2){
        left = w2-w1-10;
        flag = true;
    }
    if(flag){
        $(this).dialog('move',{left: left, top: top});
    }
};
zUtil.dialog = function(id,type,obj){
     return $(id).dialog(type);
}
